<template>
	<view class="cantainer">
		<user-info-com :userInfo="userInfo"></user-info-com>
		<!-- 组局 -->
		<partyEntrance :list="partyList" :formatBetween="formatBetween" :membersList="membersList"
			@goToNewParty="goToNewParty" @handlePartyItem="handlePartyItem" @handleNoticeBar="handleNoticeBar">
		</partyEntrance>
		<!-- 功能栏 -->
		<view class="functionWp " v-if="functionList && functionList.length>0">
			<view class="titleWp titleWp2">
				<view class="title">
					{{functionTitle}}
				</view>
			</view>
			<view class="itemWp">
				<view class="item" v-for="(item ,index) in  functionList" :key="index" @click="goToByUrl(item.url)">
					<view class="imgWp">
						<img :src="item.icon" alt="">
					</view>
					<view class="textWp" hover-class="itemHoverClass">
						{{item.name}}
					</view>
				</view>
				<!-- 客服功能按钮 -->
				<view class="item contactItem">
					<view class="imgWp">
						<img src="https://file.casugn.com/storecompcard/temp/block@2x_1688698327832.png" alt="">
					</view>
					<view class="textWp" hover-class="itemHoverClass">
						客服
					</view>
					<button class="contactBtn" open-type="contact"></button>
				</view>
			</view>
		</view>
		<view class="contactTitle">客服在线时间9:00-17:30</view>
	</view>
</template>

<script>
	import userInfoCom from '@/pages/user/components/userInfo.vue'
	import partyEntrance from '@/pages/user/components/partyEntrance.vue'
	import {
		mapState,
		mapGetters
	} from 'vuex'
	export default {
		components: {
			userInfoCom,
			partyEntrance
		},
		data() {
			return {
				joinPartyNumber: 0,
				chatMessageNumber: 0,
				quickChatNumber: 0,
				openId: '',
				noCreatedCardInfo: null,
				functionList: [],
				partyList: [],
				functionTitle: '',
				formatBetween: '',
				membersList: [],
				noticeInfo: {}
			};
		},
		computed: {
			//	...mapState(['isCheckingType']),
			...mapGetters(['userInfo', 'getHaveSupplier', ])
		},

		onLoad() {
			let data = uni.getStorageSync('openId')
			if (data.openid) {
				this.openId = data.openid
			}
			this.queryFunctionList()
		},
		onShow() {
			this.queryCurrentCardByOpenId()
			this.getClockparty()

		},
		methods: {
			//跳转创建名片
			createCard() {
				uni.navigateTo({
					url: '/pagesCard/createCard/createCard'
				})
			},
			//获取功能列表
			async queryFunctionList() {
				let res1 = await this.$api.queryUserAuthority({
					openId: this.openId
				})
				let res2 = await this.$api.queryWaterAuth({
					openId: this.openId
				})
				let res = await this.$api.queryFunctionList()
				if (res.success) {
					res.result.forEach(item => {
						if (item.category == '功能') {
							this.functionList = item.ebcFunctionList || []
							this.functionTitle = item.category
							if (!res1.result) {
								this.functionList.map((i, o) => {
									if (i.name == '房车') {
										this.functionList.splice(o, 1) // 删除房车功能项
									}
								})
							}
							if (!res2.result) {
								this.functionList = this.functionList.filter(it => it.name !== '富氢水订单')
							}
						}
						if (item.category == '名片') {
							this.mangerList = item.ebcFunctionList || []
						}
						if (item.category == '组局') {
							this.partyList = item.ebcFunctionList || []
						}
					})
				}
			},
			goToNewParty() {
				if (!this.userInfo.id) {
					uni.hideLoading()
					return uni.showToast({
						title: '请先创建名片',
						icon: 'none'
					});
				}
				let url = '/pagesParty/party/newParty/newParty'
				uni.navigateTo({
					url
				})
			},
			handlePartyItem({
				url
			}) {
				if (!this.userInfo.id) {
					uni.hideLoading()
					return uni.showToast({
						title: '请先创建名片',
						icon: 'none'
					});
				}
				const pageNum = getCurrentPages()
				if (pageNum.length > 9) {
					//当打开10个页面时，用redirectTo
					uni.redirectTo({
						url
					})
				} else {
					uni.navigateTo({
						url
					})
				}
			},
			goToByUrl(url) {
				console.log(url);
				uni.hideLoading()
				if (this.userInfo.id) {
					switch (url) {
						case '/pagesBusiness/shopMail/shopMail':
							this.goToShopList()
							break;
						case `/pagesParty/companyManger/companyManger`:
							this.goToCompanyManger()
							break;
						case '/pagesCard/quickGroup/quickGroup':
							this.goToQuickGroup()
							break;
						case '/pagesBusiness/mySupplyGoods/mySupplyGoods':
							this.goToSupplyGoods()
							break;
						default:
							const pageNum = getCurrentPages()
							if (pageNum.length > 9) {
								//当打开10个页面时，用redirectTo
								uni.redirectTo({
									url
								})
							} else {
								uni.navigateTo({
									url
								})
							}
							break;
					}
				} else {
					switch (url) {
						case '/pagesCard/quickGroup/quickGroup':
							this.goToQuickGroup()
							break;
						case '/pagesMall/mallCenter/mallCenter':
							uni.navigateTo({
								url
							})
							break;
						default:
							uni.showToast({
								title: '请先创建名片',
								icon: 'none'
							});
							break;
					}

				}

			},
			//如果存在供应商信息及id则直接跳转供应产品列表
			goToSupplyGoods() {
				if (this.getHaveSupplier) {
					uni.navigateTo({
						url: '/pagesBusiness/mySupplyGoods/mySupplyGoods'
					})
				} else {
					uni.navigateTo({
						url: '/pagesBusiness/supply/supply?path=user'
					})
				}
			},
			//获取cardId
			getCardId() {
				let cardId = uni.getStorageSync('cardId')
				if (cardId) {
					this.cardId = cardId
					this.noCreatedCardInfo = null
				} else {
					console.log('获取cardId失败');
					this.noCreatedCardInfo = '请先创建名片'
				}
			},
			//通过openId查询名片信息
			async queryCurrentCardByOpenId() {
				let data = uni.getStorageSync('openId')
				if (data.openid) {
					this.openId = data.openid
				}
				let res = await this.$api.queryCurrentCardByOpenId({
					openId: this.openId
				})
				if (res.success && res.result.id) {
					//	console.log('查询名片信息', res.result);
					this.noCreatedCardInfo = null
					this.$store.commit('userInfo', res.result)
					this.$store.commit('setIsSelf', true)
				} else {
					this.noCreatedCardInfo = '请先创建名片'
				}

			},
			//跳转商城中心
			goToShopList() {
				uni.setStorageSync('yellowCarStatus', 0)
				uni.navigateTo({
					url: '/pagesBusiness/shopMail/shopMail'
				})
			},
			// 跳转小黄车
			goTogoodsList() {
				uni.navigateTo({
					url: '/pagesBusiness/yellowCarList/yellowCarList'
				})
			},

			//快速建群
			goToQuickGroup() {
				//有名片存在
				if (this.userInfo.openId && this.userInfo.id) {
					uni.navigateTo({
						url: '/pagesCard/quickGroup/quickGroup?openId=' + this.userInfo.openId,
					})
				} else {
					//没有名片
					uni.login({
						provider: 'weixin',
						onlyAuthorize: true,
						success: async (loginRes) => {
							let code = loginRes.code
							let payload = {
								code
							}
							let res = await this.$api.getOpenIdByCode(payload);
							if (res) {
								let openId = res.openid
								uni.navigateTo({
									url: '/pagesCard/quickGroup/quickGroup?openId=' +
										openId,
								})
								console.log('没有名片,接口获取openId跳转,openID:', openId);

							} else {
								uni.showToast({
									title: '获取用户信息失败,请检查网络',
									icon: 'none'
								});
							}
						}
					});
				}



			},
			//查询待参加组局的数量徽标
			async queryJoinPartyNumber() {
				let res = await this.$api.waitJoinGroupInfo({
					openId: this.openId
				})
				if (res.success) {
					this.joinPartyNumber = res.result.length ? res.result.length : 0
				} else {
					this.joinPartyNumber = 0
				}

			},
			// 查询最近一次待参加组局
			async getClockparty() {
				let res = await this.$api.getClockparty({
					openId: this.openId
				})
				console.log(res, '查询最近一次待参加组局');
				if (res.success) {
					let {
						formatBetween,
						membersList
					} = (res.result || {})
					this.noticeInfo = res.result || {}
					this.formatBetween = formatBetween || ''
					this.membersList = membersList || []

				} else {
					this.formatBetween = ''
					this.membersList = []
				}
			},
			//点击提醒条 跳转详情
			handleNoticeBar() {
				//to do 组局详情跳转
				// const {} = this.noticeInfo
				uni.navigateTo({
					url: `/pagesParty/party/joinParty/joinParty`
				})
			},
			//查询未读聊天信息的数量徽标
			async queryChatMessageNumber() {
				let res = await this.$api.queryChatUnReadNumber({
					openId: this.openId,
				})
				if (res.success) {
					console.log('查询未读聊天信息的数量徽标', res.result);
					this.chatMessageNumber = res.result.numberChat ? res.result.numberChat : 0
					this.quickChatNumber = res.result.numberTxl ? res.result.numberTxl : 0
					if (Number(this.chatMessageNumber) > 99) {
						this.chatMessageNumber = 99
					}
					if (Number(this.quickChatNumber) > 99) {
						this.quickChatNumber = 99
					}
				} else {
					this.chatMessageNumber = 0
					this.quickChatNumber = 0
				}

			},
			async goToCompanyManger() {
				if (!this.userInfo || !this.userInfo.companyId) {
					// 企业认证为申请状态时,用户信息companyId为null
					uni.showLoading({
						mask: true,
						title: '请稍后'
					})
					let res = await this.$api.getCompanyByCardId({
						cardId: this.userInfo.id
					})
					uni.hideLoading()
					if (res.success && res.result && res.result.id) {
						// 注意:如果审核通过,一定不能传值
						uni.navigateTo({
							url: `/pagesParty/companyManger/companyManger?auditCompanyId=${res.result.status===1?'':res.result.id}`,
						})
					} else {
						uni.navigateTo({
							url: `/pagesParty/companyAttestationAdd/companyAttestationAdd?currentCardId=${this.userInfo.id}`,
						})
					}
					return
				}

				if (this.userInfo.companyId && this.userInfo.roleIdentify === 0) {
					uni.showToast({
						title: '需要管理员权限',
						icon: 'none'
					})
					return
				}

				uni.navigateTo({
					url: `/pagesParty/companyManger/companyManger`,
				})
			},

		},

	}
</script>

<style lang="scss" scoped>
	.cantainer {
		min-height: 100vh;
		background: #f7f8fa;

		.contactTitle {
			text-align: center;
			height: 50rpx;
			line-height: 50rpx;
			font-size: 24rpx;
			color: #333;
			letter-spacing: 8rpx;
		}
	}



	.badgeImgWp {
		margin: 0 auto;
		width: 70upx;
		height: 70upx;
		position: relative;

		.badgeNumber {
			width: 32upx;
			height: 32upx;
			background-color: red;
			color: #fff;
			border-radius: 50%;
			position: absolute;
			font-size: 24upx;
			line-height: 32upx;
			top: -16upx;
			right: -16upx;
		}

		.badgeImg {
			width: 70upx;
			height: 70upx;
		}
	}


	.functionWp {
		background-color: #fff;
		margin-top: 24rpx;
		width: 100%;

		.titleWp {
			padding: 24rpx 32rpx;
			height: 96rpx;
			line-height: 96rpx;
			font-size: 32rpx;
			font-weight: 600;
			color: #1D2129;
		}

		.titleWp2 {
			padding: 0 32rpx;
		}

		.contactItem {
			position: relative;

			.contactBtn {
				width: 100rpx;
				height: 132rpx;
				position: absolute;
				top: 24rpx;
				left: 26rpx;
				opacity: 0;
			}
		}


		.itemWp {
			box-sizing: border-box;
			display: flex;
			align-items: center;
			flex-wrap: wrap;
			width: 100%;

			.itemHoverClass {
				text-shadow: 1px 1px 2px #020304;
			}

			.item {
				box-sizing: border-box;
				width: 20%;
				height: 178rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;

				.imgWp {
					width: 56rpx;
					height: 56rpx;
					position: relative;

					img {
						width: 100%;
						height: 100%;
					}

					.numberWp {
						position: absolute;
						top: -7rpx;
						right: -10rpx;
						width: 32upx;
						height: 32upx;
						background-color: red;
						color: #fff;
						text-align: center;
						border-radius: 50%;
						font-size: 22upx;
						line-height: 32upx;
					}
				}

				.textWp {
					text-align: center;
					margin-top: 16rpx;
					height: 42rpx;
					font-size: 26rpx;
					font-weight: 400;
					color: #1D2129;
					line-height: 42rpx;

				}
			}
		}
	}





	.userWp {

		padding-bottom: 180upx;
		background-color: #F8F9FF;

		.user {
			overflow: hidden;
			padding: 40rpx 30rpx;


		}


	}
</style>